// Created date: 01 October, 2008
// Author: Khanh Le Ngoc Quoc (http://blog.khanhlnq.com).
// Data provided by NASA (http://jpl.nasa.gov), Custom Weather (http://customweather.com) and CIREN Vietnam (http://www2.hcm.ciren.gov.vn/cirengis).
// Powered by OpenLayers (http://www.openlayers.org).

// Tip: put link to this JS file right before </head> tag for better integration

// Create stylesheet
try {
    if(document.createStyleSheet) {
        document.createStyleSheet('http://openlayers.org/api/theme/default/style.css');
    } else {
        var styles = "@import url('http://openlayers.org/api/theme/default/style.css');";
        var newSS=document.createElement('link');
        newSS.rel='stylesheet';
        newSS.href='data:text/css,'+escape(styles);
        document.getElementsByTagName("head")[0].appendChild(newSS);
    }

    // This is to fix image border problem inside Blogger post
    if ( document.styleSheets ) {
        var nAgt = navigator.userAgent;
        //DOM stylesheets are available
        if ((verOffset=nAgt.indexOf("MSIE"))!=-1) {
            // Internet Explorer
            document.styleSheets[0].addRule("#vnmap img", "padding:0px; border:0px;");
        } else {
            document.styleSheets[0].insertRule('#vnmap img {padding:0px; border:0px;}', 0);
        }
    }
} catch(err) {
    // Do nothing
}

// Map manipulating
var vnmap;

// avoid pink tiles
OpenLayers.IMAGE_RELOAD_ATTEMPTS = 3;
OpenLayers.Util.onImageLoadErrorColor = "transparent";
OpenLayers.ProxyHost = "http://openlayers.org/proxy/?url=";

function initVietnamMap() {
    try {
        vnmap = new OpenLayers.Map('vnmap', {controls : []});

        var basiclayer = new OpenLayers.Layer.WMS.Untiled( "OpenLayers WMS",
                "http://labs.metacarta.com/wms/vmap0", {layers: 'basic'}, {singleTile: true} );

        var nasawms = new OpenLayers.Layer.WMS.Untiled(
            "NASA Map",
            "http://wms.jpl.nasa.gov/wms.cgi",
            {layers: 'BMNG'},
            {singleTile: true}
        );

        var customweatherwms = new OpenLayers.Layer.WMS.Untiled(
            "Custom Weather Map",
            "http://maps.customweather.com/image?client=ucl_test&client_password=t3mp",
            {
                transparent: 'TRUE',
                layers: 'country_borders,geom,severe_warnings,label,anim,legend,world_borders,hurricanes,contour_pressure,storm_track,synoptic_weather,global_ir_satellite_10km',
                singleTile: true
            }
        );
        
        /* var yahooweather = new OpenLayers.Layer.GML("Yahoo! Weather", "http://www.wis.ro/cache/WISroGIS_YWKML/WISroGIS_YW.kml", 
                   {
                    format: OpenLayers.Format.KML, 
                    formatOptions: {
                      extractStyles: true, 
                      extractAttributes: true
                    },
                    projection: new OpenLayers.Projection("EPSG:4326")
                   }); */
                   
        var mymarker = new OpenLayers.Icon("http://pages.khanhlnq.com/marker.png", new OpenLayers.Size(21,25));
                   
        var hcmweather = new OpenLayers.Layer.GeoRSS("Ho Chi Minh city weather", "http://weather.yahooapis.com/forecastrss?p=VMXX0007&u=c", {'icon':mymarker});
        var hanoiweather = new OpenLayers.Layer.GeoRSS("Ha Noi weather", "http://weather.yahooapis.com/forecastrss?p=VMXX0006&u=c", {'icon':mymarker});
        
        var vietnamwms = new OpenLayers.Layer.WMS.Untiled(
            "Vietnam Map",
            "http://www2.hcm.ciren.gov.vn/geoserver/wms",
            {
                transparent: 'TRUE',
                layers: 'webgis:province_city,webgis:vn_sea',
                singleTile: true
            }
        );

        vnmap.addLayers([nasawms, basiclayer, customweatherwms, hcmweather, hanoiweather, vietnamwms]);

        vnmap.setCenter(new OpenLayers.LonLat(lon,lat), mapzoom);
        // vnmap.addControl( new OpenLayers.Control.LayerSwitcher() );
        // vnmap.addControl( new OpenLayers.Control.PanZoomBar() );
        // vnmap.addControl(new OpenLayers.Control.Permalink());
        // vnmap.addControl(new OpenLayers.Control.MousePosition());
    } catch(err) {
    // Do nothing
    }
}

// Initialize Vietnam map
initVietnamMap();